import jdk.internal.util.xml.impl.Input;

import javax.jnlp.DownloadService;
import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

public class Main {


    static String[] password = new String[2];
    static Scanner in = new Scanner(System.in);

    static HashMap<String,User> hashMap = new HashMap<>();

    public static void main(String[] args) {

        password[0] = "123456";
        password[1] = "062199";

        User normalUser = new User(1,"小白",false);

        hashMap.put(password[0],normalUser);

        User vipUser = new User(2,"bxw",true);

        hashMap.put(password[1],vipUser);


        System.out.println("选择要登录的用户：1 普通用户  2 VIP用户");
        int num = in.nextInt();
        in.nextLine();

        System.out.println("输入密码");
        String inputPassword = in.nextLine();



        if(login(num,inputPassword)) {

            User nowUser = hashMap.get(inputPassword);

            System.out.println("开始下载文件");

            download(nowUser);

        }else {
            System.out.println("重新登录");
        }



        // 读写文件

        // 判断用户是否为vip



//        try {
//            String inputPath = "E:\\javacode\\javalearning\\demo\\test.txt";
//
//            InputStream inputStream = new FileInputStream(inputPath);
//
//            ObjectInput objectInput = new ObjectInputStream(inputStream);
//
//            Person checkp1 = (Person) objectInput.readObject();
//
//            System.out.println("反序列化完成");
//
//            System.out.println(checkp1.getAge());
//            System.out.println(checkp1.getName());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }


//        Person p1 = new Person(18,"小白");
//
//        try {
//
//            String outputPath = "E:\\javacode\\javalearning\\demo\\test.txt";
//
//            OutputStream outputStream = new FileOutputStream(outputPath);
//
//            ObjectOutput objectOutput = new ObjectOutputStream(outputStream);
//
//            objectOutput.writeObject(p1);
//
//            System.out.println("储存为文件");
//
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }


//        try {
//            InputStream inputStream = new FileInputStream("E:\\javacode\\javalearning\\demo\\bxw.jpg");
//
//            int fileSize = inputStream.available();
//
//            byte[] imgeData = new byte[fileSize];
//
//            inputStream.read(imgeData);
//
//            System.out.println("查看数据内容");
//
//            for (int i = 0; i < imgeData.length; i++) {
//                System.out.print(imgeData[i]);
//            }
//
//            String outputStreamPath = "E:\\javacode\\javalearning\\demo\\next.jpg";
//
//            //检查该位置是否有文件存在
//
//            InputStream check = new FileInputStream(outputStreamPath);
//
//            if (check.read() != -1) {
//                System.out.println("该位置已有文件");
//            }else {
//                OutputStream outputStream = new FileOutputStream(outputStreamPath);
//
//                outputStream.write(imgeData);
//
//                System.out.println("未捕获到异常");
//            }
//
//        } catch (IOException e) {
//            System.out.println("捕获到异常");
//        }
//
//        System.out.println("释放资源");


//        int pre = -1;
//
//        while ((pre = inputStream.read()) != -1) {
//            System.out.println((char) pre);
//        }

//        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream());
//
//        String str = new String(bufferedInputStream)
    }

    public static void download(User nowUser) {
        String filePath = "E:\\javacode\\javalearning\\demo\\简历.pdf";

        String deskPath = "E:\\javacode\\javalearning\\demo\\MyFile\\简历.pdf";

        long start = 0;
        long end = 0;

        if (nowUser.isVIP()) {

            try {
                InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
                OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(deskPath));

                int size = inputStream.available();

                System.out.println("文件大小" + size);

                int content;
                start = System.currentTimeMillis();
                while((content = inputStream.read()) != -1) {
                    outputStream.write(content);
                }
                end = System.currentTimeMillis();

                System.out.println("下载完成");
                System.out.println("下载所消耗的时间" + (end - start));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        }else {


            try {
                InputStream inputStream = new FileInputStream(filePath);
                OutputStream outputStream = new FileOutputStream(deskPath);

                int size = inputStream.available();

                System.out.println("文件大小" + size);

                int content;
                start = System.currentTimeMillis();
                while((content = inputStream.read()) != -1) {
                    outputStream.write(content);
                }
                end = System.currentTimeMillis();

                System.out.println("下载完成");
                System.out.println("下载所消耗的时间" + (end - start));

            } catch (IOException e) {
                System.out.println("下载失败");
                throw new RuntimeException(e);
            }

        }


    }

    public static boolean login(int num,String inputPassword) {



        if(num < 1 || num > 2) {
            System.out.println("非法输入");
            return false;
        }else {


            if(!password[num - 1].equals(inputPassword)) {
                System.out.println("密码错误");
                return false;
            }
        }
        return true;
    }
}
